package com.zc.im.common.r2dbc.event.callback;

import com.zc.im.common.r2dbc.domain.AbstractBase;
import com.zc.im.common.util.SnowFlakeUtil;
import org.reactivestreams.Publisher;
import org.springframework.data.r2dbc.mapping.OutboundRow;
import org.springframework.data.r2dbc.mapping.event.BeforeConvertCallback;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.r2dbc.core.Parameter;
import reactor.core.publisher.Mono;

/**
 * Project：im
 * Date：2022/4/20
 * Time：10:48
 * Description：转换前事件回调
 *
 * @author Challen.Zhang
 * @version 1.0
 */
public class BaseBeforeConvertCallback implements BeforeConvertCallback<AbstractBase> {

    @Override
    public Publisher<AbstractBase> onBeforeConvert(AbstractBase entity, SqlIdentifier table) {
        if (entity .isNew()){
            // 生成id
            entity.setId(SnowFlakeUtil.snowflakeId());
        }
        return Mono.just(entity);
    }
}
